package io.afu.imagehelper.utils;

import io.afu.imagehelper.dto.ImgDto;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.rmi.server.ExportException;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class ImgUtils {
    public static String getFileSuffix(String pathOrFileName) {
        String suffix = "";
        int index = pathOrFileName.lastIndexOf(".");
        if (index != -1){
            suffix = pathOrFileName.substring(index+1);
        }
        return suffix;
    }

    public static ImgDto getImgDto(String imgPath) {
        ImgDto imgDto = null;
        File file = new File(imgPath);
        if (!file.exists()) {
            return null;
        }
        try {
            imgDto = new ImgDto();
            imgDto.setFilePath(imgPath);
            BufferedImage bufferedImage = ImageIO.read(file);
            imgDto.setHeight(bufferedImage.getHeight());
            imgDto.setWidth(bufferedImage.getWidth());
            imgDto.setSuffix(getFileSuffix(imgPath));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return imgDto;
    }




}
